package com.sex.bull.melancholy.service.mybatis.tools;

import com.sex.bull.melancholy.service.mybatis.bean.doc.Columns;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成数据库表结构的数据字典
 * Created by Administrator on 2015/8/20.
 */

@Component
public class BuildDataDocHandle {
    @Autowired
    private SqlSessionTemplate sqlSession;

    /**
     * 提取数据库名
     * @return
     */
    private  List<String>  getDbName() {
        return sqlSession.selectList(Columns.class.getName() + ".selectDBName");
    }

    private List<String>  getTableName(String SchemaName) {
        return sqlSession.selectList(Columns.class.getName() + ".selectTableName",SchemaName);
    }

    private List<Columns>  getFieldName(String TableName,String DBName) {
        Map<String,String> DBdata=new HashMap<String,String>();
        DBdata.put("TableName",TableName);
        DBdata.put("DBName",DBName);

        return sqlSession.selectList(Columns.class.getName() +".selectFieldName",DBdata);
    }

    public void DBDoc4Markdown() {

        String filePath=this.getClass().getResource("/").getPath()+"DBDoc4Markdown.txt";

        File writename = new File(filePath);
        BufferedWriter out = null;
        try {
            writename.createNewFile();
            out = new BufferedWriter(new FileWriter(writename));
            out.write("#共" + getDbName().size() + "个数据库");
            out.newLine();


            StringBuilder sb = new StringBuilder();
            for (String schemaName : getDbName()) {
                out.write("#数据库：" + schemaName + "（");
                out.write("共" + getTableName(schemaName).size() + "个表）");
                out.newLine();


                for (String TableName : getTableName(schemaName)) {

                    out.write("###" + TableName.replace("_", "\\_") + " 表");
                    out.newLine();
                    out.write("<table>");
                    out.newLine();
                    out.write("<th>列名</th><th>字段类型</th><th>IS NULL</th><th>默认值</th><th>字段的额外信息</th><th>字段的备注</th>");


                    for (Columns columns : getFieldName(TableName,schemaName)) {
                        sb.delete(0, sb.length());
                        out.newLine();
                        out.write("<tr>");
                        out.newLine();
                        sb.append("<td>").append(columns.getColumnName()).append("</td><td>").append(columns.getColumnType()).append("</td><td>");
                        sb.append(columns.getIsNullable()).append("</td><td>");
                        if(StringUtils.isNoneBlank(columns.getColumnDefault())){
                            sb.append(columns.getColumnDefault());
                        }else {
                            sb.append("无");
                        }
                        sb.append("</td><td>");
                        if(StringUtils.isNoneBlank(columns.getExtra())){
                            sb.append(columns.getExtra());
                        }else{
                            sb.append(" ");
                        }
                        sb.append("</td><td>");
                        if(StringUtils.isNoneBlank(columns.getColumnComment())){
                            sb.append(columns.getColumnComment());
                        }else{
                            sb.append(" ");
                        }
                        sb.append("</td>");

                        out.write(sb.toString());
                        sb.delete(0, sb.length());
                        out.newLine();
                        out.write("</tr>");

                    }
                    out.newLine();
                    out.write("</table>");
                    out.newLine();
                    out.newLine();

                }

            }
            System.out.println(">>>最终生成的文件位置为：" + filePath);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }

}

